package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmeaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * @author Encounter
 * @date 2024/10/05 13:46<br/>
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController
    {
        @Autowired
        private SetmeaService setmeaService;
        
        @Autowired
        private AliOssUtil aliOssUtil;
        
        @Autowired
        private RedisTemplate redisTemplate;
        
        /**
         * 新增套餐
         *
         * @param setmeal       套餐
         * @param checkgroupIds 检查组 ID
         * @return {@link Result }
         */
        @PostMapping("/add")
        //@CachePut(value = RedisConstant.SETMEAL_PIC_DB_RESOURCES,key = "#setmeal.img")
        public Result add(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds)
            {
                try
                    {
                        log.info("新增套餐：{},检查组id：{}", setmeal, checkgroupIds);
                        setmeaService.add(setmeal, checkgroupIds);
                        // 将图片资源存入缓存
                        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
                        
                        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
                    }
            }
        
        /**
         * 找到由id
         *
         * @param setmealId 套餐id
         * @return {@link Result }
         */
        @GetMapping("/findById")
        public Result findById(@RequestParam Integer setmealId)
            {
                try
                    {
                        log.info("要查询的套餐id：{}", setmealId);
                        Setmeal setmeal = setmeaService.findById(setmealId);
                        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
                    }
            }
        
        /**
         * 编辑套餐
         *
         * @param setmeal 套餐
         * @return {@link Result }
         */
        @PutMapping("/edit")
        public Result edit(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkGroupIds)
            {
                try
                    {
                        log.info("编辑套餐：{}，修改的检查组id：{}", setmeal, checkGroupIds);
                        setmeaService.edit(setmeal, checkGroupIds);
                        // 将图片资源存入缓存
                        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
                        
                        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
                    }
                
            }
        
        /**
         * 动态分页查询
         *
         * @param queryPageBean 查询页面 Bean
         * @return {@link Result }
         */
        @PostMapping("/findPage")
        public Result findPage(@RequestBody QueryPageBean queryPageBean)
            {
                try
                    {
                        log.info("分页查询参数：{}", queryPageBean);
                        PageResult pageResult = setmeaService.findPage(queryPageBean);
                        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
                    }
            }
        
        /**
         * 删除由id
         *
         * @param setmealId 套餐id
         * @return {@link Result }
         */
        @DeleteMapping("/delete")
        public Result deleteById(@RequestParam Integer setmealId)
            {
                try
                    {
                        log.info("要删除的id：{}", setmealId);
                        setmeaService.deleteById(setmealId);
                        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
                    }
            }
        
        /**
         * 上传图片
         *
         * @param imgFile img 文件
         * @return {@link Result }
         */
        @PostMapping("/upload")
        //@CachePut(value = RedisConstant.SETMEAL_PIC_RESOURCES,key = "#result.data")
        public Result upload(MultipartFile imgFile)
            {
                try
                    {
                        log.info("上传的文件：{}", imgFile);
                        //获取原始文件名
                        String originalFilename = imgFile.getOriginalFilename();
                        //获取原始文件后缀
                        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                        //更改文件名，使其变为唯一，不会重复
                        String newName = UUID.randomUUID() + suffix;
                        //上传阿里云OSS并回传上传地址
                        String uploadPath = aliOssUtil.upload(imgFile.getBytes(), newName);
                        // 将图片资源存入缓存
                        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES, uploadPath);
                        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, uploadPath);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
                    }
            }
    }
